﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace ImageConverter.Configuration
{
    public class XmlConfiguration : IConfiguration
    {
        public XmlConfiguration(string fileName)
        {
            if ( !File.Exists(fileName))
                throw new FileNotFoundException("File not found", fileName);

            XDocument doc = XDocument.Load(fileName);
            ParseAspectsSection(doc);
        }

        #region IConfiguration Members

        public IEnumerable<AspectSection> Aspects { get; private set; }

        #endregion

        #region Private methods

        private void ParseAspectsSection(XDocument doc)
        {
            List<AspectSection> aspectsBuf = new List<AspectSection>();
            foreach (var a in doc.Descendants("aspect"))
            {
                aspectsBuf.Add(new AspectSection()
                {
                    Aspect = Double.Parse(a.Attribute("key").Value),
                    Width = Int32.Parse(a.Attribute("width").Value),
                    Height = Int32.Parse(a.Attribute("height").Value)
                });
            }
            Aspects = aspectsBuf;
        }

        #endregion
    }
}
